/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform.xsd;
import java.io.File;
//import java.io.IOException;
import java.io.StringReader;

import org.w3c.dom.Element;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
//import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

//import com.sun.xml.xsom.impl.ElementDecl;
import com.sun.xml.xsom.parser.XSOMParser;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSSchemaSet;


public class XsdInputDocument2 {
	public XsdInputDocument2(File pInputFile) {
		XSOMParser parser = new XSOMParser();
		//parser.setErrorHandler(...);
		//parser.setEntityResolver(...);

		try {
			parser.parse( pInputFile );	
			mSchemaSet = parser.getResult();
		} catch (Exception e) {
			e.printStackTrace();
			mSchemaSet = null;
		}
	}
	
	public XsdInputDocument2(String pPath, Element pSchemaElem, Element[] pSchemas) {
		XSOMParser parser = new XSOMParser();
		try {
			parser.setErrorHandler(new ErrorHandler() {
				public void warning(SAXParseException e) {
					e.printStackTrace();
				}
				public void error(SAXParseException e) {
					System.out.println("ERROR. Col=" + e.getColumnNumber() + " Line=" + e.getLineNumber() + " (public id=" + e.getPublicId() + ")");
					e.printStackTrace();
				}
				public void fatalError(SAXParseException e) {
					e.printStackTrace();
				}
			});
			mSchemas = pSchemas;
			parser.setEntityResolver(new EntityResolver() {
		       public InputSource resolveEntity(String publicId, String systemId) {
		    	   
		    	   for (int i = 0; i < mSchemas.length; i++) {
		    		   
		    		   if (mSchemas[i].getAttribute("targetNamespace").equals(publicId)) {
		    			   return new InputSource(new StringReader(mSchemas[i].toString()));
		    		   }
		    	   }
		           return null;
		       }
			});
			InputSource schemaInSrc = new InputSource(new StringReader(pSchemaElem.toString()));
			schemaInSrc.setSystemId(pPath);
			parser.parse(schemaInSrc);
			mSchemaSet = parser.getResult();
		} catch (SAXParseException e) {
			System.out.println("ERROR. Col=" + e.getColumnNumber() + " Line=" + e.getLineNumber() + " (public id=" + e.getPublicId() + ")");
			e.printStackTrace();
			mSchemaSet = null;
		} catch (Exception e) {
			e.printStackTrace();
			mSchemaSet = null;
		}
	}
	
	public boolean isValid() {
		return (mSchemaSet != null);
	}
		
	public XSElementDecl getElement(String pNamespace, String pName) {
		return mSchemaSet.getElementDecl(pNamespace, pName);
	}
	
	public XSComplexType getComplexType(String pNamespace, String pName) {
		return mSchemaSet.getComplexType(pNamespace, pName);
	}
	
	private XSSchemaSet mSchemaSet;
	private Element[] mSchemas;
}
